local tianyi = fk.CreateSkill {
  name = "iberia__tianyi"
}

local I = require "packages/iberia/utility"

Fk:loadTranslationTable {
  [tianyi.name] = "天义",
  [":"..tianyi.name] = "出牌阶段限一次，你可以与一名角色拼点："
  .."若势力与你相同的角色赢，本阶段你使用【杀】的次数上限、目标上限+1且无距离限制；"
  .."若其他势力角色赢，本阶段你不能使用【杀】。",
  ["#"..tianyi.name] = "天义：选择一名角色，与其拼点。",
}

tianyi:addEffect("active", {
  anim_type = "special",
  prompt = "#"..tianyi.name,
  max_phase_use_time = 1,
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return not player:isKongcheng() and player:usedSkillTimes(tianyi.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = I.pindian(player, {target}, tianyi.name)
    if player.dead then return end
    local winner = I.getPindianWinner(pindian)
    if winner then
      if I.isMate(winner, player) then
        room:addPlayerMark(player, "iberia__tianyi_win-phase", 1)
      else
        room:addPlayerMark(player, "iberia__tianyi_lose-phase", 1)
      end
    end
  end,
})

tianyi:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and player:getMark("iberia__tianyi_win-phase") > 0 and scope == Player.HistoryPhase then
      return 1
    end
  end,
  bypass_distances =  function(self, player, skill)
    return skill.trueName == "slash_skill" and player:getMark("iberia__tianyi_win-phase") > 0
  end,
  extra_target_func = function(self, player, skill)
    if skill.trueName == "slash_skill" and player:getMark("iberia__tianyi_win-phase") > 0 then
      return 1
    end
  end,
})

tianyi:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    return player:getMark("iberia__tianyi_lose-phase") > 0 and card.trueName == "slash"
  end,
})

return tianyi